package browse

import (
	"organoid_public/domain/sample"
	"organoid_public/domain/sample/repository"
	sampleService "organoid_public/domain/sample/service"
	"organoid_public/infrastructure/db/gormclient"
	"organoid_public/pkg/logger"
	"organoid_public/pkg/valueobject"
)

type service struct {
	log           logger.Logger
	browseService sampleService.BrowseServiceInterface
}

func newService(log logger.Logger) (*service, error) {
	uc, err := sample.Service()
	if err != nil {
		return nil, err
	}
	return &service{
		log:           log,
		browseService: uc.BrowseSvc,
	}, nil
}

func (s *service) GetGeneralList(params *GeneralListReq) (*gormclient.PageList[GeneralListRes], error) {
	data, err := s.browseService.GeneralList(&repository.GeneralListParams{
		Keywords:          params.Keywords,
		Pagination:        valueobject.NewPagination(params.Page, params.PageSize),
		Organoid:          params.Organoid,
		Origin:            params.Origin,
		Species:           params.Species,
		CultureTechnique:  params.CultureTechnique,
		Biomarker:         params.Biomarker,
		Applications:      params.Applications,
		Test:              params.Test,
		CultureConditions: params.CultureConditions,
		Endpoints:         params.Endpoints,
		Characteristics:   params.Characteristics,
		Functions:         params.Functions,
		DiseaseModeling:   params.DiseaseModeling,
	})
	if err != nil {
		return nil, err
	}
	list := make([]GeneralListRes, 0, len(data.List))
	for _, item := range data.List {
		list = append(list, GeneralListRes{
			SampleId:         item.SampleId,
			Organoid:         item.Organoid,
			Origin:           item.Origin,
			Species:          item.Species,
			CultureTechnique: item.CultureTechnique,
			CultureDays:      item.CultureDays,
			Biomarker:        item.Biomarker,
			Applications:     item.Applications,
			GeneEditing:      item.TestGeneEditing,
			Imaging:          item.TestImaging,
			MultiOmics:       item.TestMultiOmics,
			CoCulture:        item.TestCoCulture,
			Xenograft:        item.TestXenograft,
			Endpoints:        item.Endpoints,
			Characteristics:  item.Characteristics,
			Functions:        item.Functions,
			DiseaseModeling:  item.DiseaseModeling,
			References:       item.Reference,
		})
	}

	return &gormclient.PageList[GeneralListRes]{
		Page:     data.Page,
		PageSize: data.PageSize,
		Total:    data.Total,
		List:     list,
	}, nil
}
